package com.ssm.zy.configuration;

import com.ssm.zy.handler.WebSocketDoubleChatHandler;
import com.ssm.zy.handler.WebSocketMultipleChatHandler;
import com.ssm.zy.interceptor.WebSocketInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * @author 郑智辰~
 * @version 1.0
 *
 * WebSocket配置类(注册WebSocket处理器)
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private WebSocketMultipleChatHandler webSocketMultipleChatHandler;

    @Resource
    private WebSocketDoubleChatHandler webSocketDoubleChatHandler;

    @Resource
    private WebSocketInterceptor webSocketInterceptor;

    // 注册WebSocket处理器 以及 握手拦截器
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(webSocketMultipleChatHandler,"/ws/xhs")
                                .addInterceptors(webSocketInterceptor)
                                .setAllowedOrigins("*");

        webSocketHandlerRegistry.addHandler(webSocketDoubleChatHandler,"/xhs-double/{username}")
                                .addInterceptors(webSocketInterceptor)
                                .setAllowedOrigins("*")
                                .withSockJS();
    }
}
